package com.shoulder.authorizationserver.rsa;

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;

import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.time.Duration;
import java.time.Instant;

public class InitRsaKeyPairs implements ApplicationRunner {
    private final RsaKeyPairRepository repository;
    private final String id;
    private final RSAPublicKey publicKey;
    private final RSAPrivateKey privateKey;

    public InitRsaKeyPairs(RsaKeyPairRepository repository, String id, RSAPublicKey publicKey, RSAPrivateKey privateKey) {
        this.repository = repository;
        this.id = id;
        this.publicKey = publicKey;
        this.privateKey = privateKey;
    }

    @Override
    public void run(ApplicationArguments args) {
        this.repository.save(new RsaKeyPair(this.id, Instant.now().minus(Duration.ofDays(1)), publicKey, privateKey));
    }

}